from src.util.Service import *


# 获取所有的日志信息(分页 所有信息)
def getLogAll(page: Page = Page(), h_type: str = '', cdn=None):
    if cdn is None:
        cdn = dict()
    if h_type == '':
        return Log.getAllHandleLog(page, cdn)
    h = SysHandle.getHandleNameByType(h_type)
    hids = [item.get('hId') for item in h]
    return Log.getAllHandleLog(page, cdn, hids)


# 日志导出
def exportLog(h_type: str = '', cdn: dict = None):
    if cdn is None:
        cdn = {}
    h = SysHandle.getHandleNameByType(h_type)
    hids = [item.get('hId') for item in h]
    return Log.getAllHandleLog(cdn=cdn, ids=hids)


# 修改日志信息（除了lid）
def updateLog(id: int, k):
    return Log.updateHandleLog(id, k)


# 删除日志信息
def delLog(lids: list[int]):
    return Log.clearLog(lids)


# 清空日志
def clearLog(h_type: str, cdn: dict):
    h = SysHandle.getHandleNameByType(h_type)
    hids = [item.get('hId') for item in h]
    Log.clearLog(hids, cdn)
    return True


# 增加日志信息
def addLog(username, token, resp, status, ip, path, req, method):
    handle: dict = SysHandle.getHandleByPathAndMethod(path, method)
    if handle is None:
        return
    if handle.get("status") == "0":
        return
    if handle.get("log") == "0":
        return
    log = {
        "handleName": handle.get("name"),
        "time": datetime.utcnow(),
        "key": handle.get("key"),
        "resp": resp,
        "username": username,
        "status": status,
        "ip": ip,
        "token": token,
        "path": path,
        "req": req,
        "hid": handle.get('hId')
    }
    return Log.addHandleLog(log)
